public class Main {
    public static void main(String[] args) {
        System.out.println(new Solution().monotoneIncreasingDigits(10));
        System.out.println(new Solution().monotoneIncreasingDigits(1234));
        System.out.println(new Solution().monotoneIncreasingDigits(332));
    }
}


class Solution {
    public int monotoneIncreasingDigits(int n) {
        char[] strN = Integer.toString(n).toCharArray();
        int i = 0;
        while (i < strN.length - 1 && strN[i] <= strN[i + 1]) {
            i++;
        }

        if (i < strN.length - 1) {
            while (i >= 0 && strN[i] > strN[i + 1]) {
                strN[i]--;
                i--;
            }

            i += 2;
            while (i < strN.length) {
                strN[i] = '9';
                i++;
            }
        }
        return Integer.parseInt(new String(strN));
    }
}
